<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2016/9/28
  Time: 15:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <jsp:include page="../../head.jsp"/>
    <title>文件上传页面</title>
    <script type="application/javascript" src="../../js/gtimp3/declaration.js"></script>
    <script type="text/javascript">
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }
        var formSetting = {
            title: "附件操作",
            module: 'query',
            numCount: '2',
            columns: [],
            buttons: {
                custom: []
            },
            grid: {
                id: "dataGrid",
                title: "查询结果",
                url: 'attach/findListByBillNo?type=' + ((getQueryString('type') ? getQueryString(
                        'type') : '')) + '&bill_no=' + location.hash.substr(1),
                colModel: [{
                    index: "id",
                    name: "id",
                    label: "id",
                    hidden: true
                }, {
                    index: "name",
                    name: "name",
                    label: "文件名"
                }, {
                    index: "ctime",
                    name: "ctime",
                    formatter: 'date',
                    formatoptions: {
                        srcformat: 'Y-m-d H:i:s',
                        newformat: 'Y-m-d H:i:s'
                    },
                    label: "上传时间"
                }, {
                    index: "opa",
                    name: "id",
                    label: "操作"
                }],
                sortname: "ctime",
                sortorder: "desc",
                jsonReader: {
                    // root: "attachments"
                },
                loadComplete: function (data) {
                    $("td[aria-describedby='dataGrid_id'][style!='display:none;']")
                            .each(
                                    function () {
                                        var html = "<a href='javascript:doDownload(\""
                                                   + $(this).text()
                                                   + "\",\"${param.type}\")'>下载</a>&nbsp;&nbsp;&nbsp;&nbsp;";

                                        var html2 = ((self.parent.location.pathname
                                                      != '/registered/approve-platformApprove' && self.parent.location.pathname != '/business/cominfo/toAuditCom')
                                                ? ("<a href='javascript:doDelUploadFile(\""
                                                   + $(this).text()
                                                   + "\")'>删除</a>") : "");
                                        if (top.window.location.pathname.indexOf('reg-reg') == -1) {
                                            if (getQueryString("hide")) {
                                                $(this).html(html);
                                            }
                                            else {
                                                $(this).html(html + html2);
                                            }
                                        }
                                        else {
                                            $(this).html(html2);
                                        }
                                    })
                }
            }
        };
        function doDelUploadFile(id) {
            if (id) {
                jw.confirm("确认删除记录？", function () {
                    $.get("attach-del?id=" + id + "&type=${param.type}", function (result) {
                        jw.alert(result.msg, {
                            onOk: function () {
                                if (result.result) {
                                    history.go(0);
                                }
                                else {
                                    //TODO
                                }
                            }
                        });
                    });
                })
            }
            else {

            }
        }
        $(document).ready(function () {
            $("#dataTable").mrdForm(formSetting);
            $("#mf_buttons_container").empty();
            $("#mf_buttons_container").css("text-align", "left");
            $("#mf_buttons_container").append($("#dialog002"));
            $("form#uploadAccessoryForm")[0].elements['bill_no'].value = location.hash.substr(1);
            $("form#uploadAccessoryForm")[0].elements['type'].value =
                    ((getQueryString('type') ? getQueryString('type') : ''));
            $("form#uploadAccessoryForm").submit(function (e) {
                e.preventDefault();
                e.target.elements[3].disabled = true;
                var formData = new FormData($(this)[0]);
                var index = layer.load(1, {
                    shade: [0.1, '#000'] //0.1透明度的白色背景
                });
                $.ajax({
                           url: $(this).attr('action'), // le nom du fichier indiqué dans le formulaire
                           type: $(this).attr('method'), // la méthode indiquée dans le formulaire (get ou post)
                           cache: false,
                           data: formData,
                           contentType: false,
                           processData: false,
                           success: function (result) {
                               /* history.go(0);*/
                               layer.close(index);
                               e.target.elements[3].disabled = false;
                               $("#dataGrid").jqGrid("getGridParam", "postData").filters = "";
                               $("#dataGrid").jqGrid().trigger("reloadGrid");
                               /*jw.alert(result.msg, {
                                onOk: function () {
                                e.target.elements[3].disabled = false;
                                if (result.result) {
                                history.go(0);
                                }
                                else {
                                TODO
                                }
                                }
                                });*/
                           },
                           error: function (XMLHttpRequest, msg, e2) {
                               e.target.elements[3].disabled = false;
                               layer.close(index);
                               e.target.elements[2].value = '';
                               jw.alert('文件大小不要超过10M。');
                           }
                       });
                return false;
            });
            if (self.parent.location.pathname == '/registered/approve-platformApprove' || self.parent.location.pathname == '/business/cominfo/toAuditCom') {
                $("#uploadAccessoryForm").hide();
            }
            if (getQueryString('no-upload')) {
                $("#uploadAccessoryForm").hide();
            }
        });
    </script>
</head>
<body>
<div id="dataTable"></div>
<div id="dialog002">
    <form id="uploadAccessoryForm" action="attach/doUpload"
          enctype="multipart/form-data" method="POST">
        <input type="hidden" name="bill_no"/>
        <input type="hidden" name="type"/>
        <input type="file" label="请" name="attach" required>
        <input type="submit" value="确认上传">
        <ol>
            <li>文件大小不要超过10M。</li>
        </ol>
    </form>
</div>
</body>
</html>
